package im.starchat.chat.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.adapter.RecommendAdapter;
import im.starchat.chat.impl.IFollowingCallback;
import im.starchat.chat.rest.RestClient;
import im.starchat.chat.rest.model.BatchCancelRequestBody;
import im.starchat.chat.rest.model.ChannelBean;
import im.starchat.chat.rest.model.api.NullResponse;

import java.util.Arrays;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;

/**
 * Created by wxz on 16/3/24.
 * 推荐页
 */
public class RecommendStarActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.top_back)
    View mTopBack;
    @InjectView(R.id.top_title)
    TextView mTopTitle;
    @InjectView(R.id.done)
    TextView mDone;
    @InjectView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @InjectView(R.id.follow_all)
    TextView mFollowAllBtn;
    @InjectView(R.id.cancel_attention)
    TextView mCancelAllBtn;

    private LinearLayoutManager mLayoutManager;
    private RecommendAdapter mRecommendAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        initView();

    }

    private void initView() {
        setContentView(R.layout.activity_guide);
        ButterKnife.inject(this);

        mTopTitle.setText("推荐关注");
        mTopBack.setOnClickListener(this);
        mDone.setOnClickListener(this);
        mFollowAllBtn.setOnClickListener(this);
        mCancelAllBtn.setOnClickListener(this);

        mRecyclerView.setHasFixedSize(true);
        mLayoutManager = new LinearLayoutManager(RecommendStarActivity.this);
        mRecyclerView.setLayoutManager(mLayoutManager);
        mRecommendAdapter = new RecommendAdapter(RecommendStarActivity.this, new IFollowingCallback() {
            @Override
            public void onFollowing(String id, int position) {
                following(id, position);
            }

            @Override
            public void onCancelFollowing(String id, int position) {
                cancelFollowing(id, position);
            }
        });
        mRecyclerView.setAdapter(mRecommendAdapter);

        getRecommendList();

    }

    private void getRecommendList(){
        XLApplication.getInstance().getRestClient().getStars("recommend", 0, RestClient.DEFAULT_PREPAGE, new Callback<ChannelBean[]>() {
            @Override
            public void success(ChannelBean[] channelBean, Response response) {
                mRecommendAdapter.refreshData(Arrays.asList(channelBean));
            }

            @Override
            public void failure(RetrofitError error) {
            }
        });
    }

    private void following(String id, final int position){
        XLApplication.getInstance().getRestClient().following(id, "", new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(RecommendStarActivity.this, "关注成功", Toast.LENGTH_SHORT).show();
                mRecommendAdapter.changeAttentionState(position, true);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    private void batchFollowing(BatchCancelRequestBody ids){
        XLApplication.getInstance().getRestClient().bratchFollowing(ids, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                if (mRecommendAdapter != null) {
                    mRecommendAdapter.changeBatchAttentionState(true);
                }
                mCancelAllBtn.setVisibility(View.VISIBLE);
                mFollowAllBtn.setVisibility(View.GONE);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    private void batchCancelFollowing(BatchCancelRequestBody ids){
        XLApplication.getInstance().getRestClient().bratchCancelFollowing(ids, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                if (mRecommendAdapter != null) {
                    mRecommendAdapter.changeBatchAttentionState(false);
                }
                mCancelAllBtn.setVisibility(View.GONE);
                mFollowAllBtn.setVisibility(View.VISIBLE);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }

    private void cancelFollowing(String id, final int position){
        XLApplication.getInstance().getRestClient().delFollowing(id, new Callback<NullResponse>() {
            @Override
            public void success(NullResponse nullResponse, Response response) {
                Toast.makeText(RecommendStarActivity.this, "取消关注成功", Toast.LENGTH_SHORT).show();
                mRecommendAdapter.changeAttentionState(position, false);
            }

            @Override
            public void failure(RetrofitError error) {

            }
        });
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.done:
            case R.id.top_back:
                finish();
                Intent intent = new Intent(RecommendStarActivity.this, Main2Activity.class);
                startActivity(intent);
                break;

            //关注全部
            case R.id.follow_all:

                if(mRecommendAdapter != null){
//                    String unFollowingStr = mRecommendAdapter.getUnFollowedIdString();
//                    System.out.println("unFollowingStr-->" + unFollowingStr);
                    BatchCancelRequestBody requestBody = new BatchCancelRequestBody(mRecommendAdapter.getUnFollowedIdInt());
                        batchFollowing(requestBody);//("{user_ids:" + unFollowingStr + "}");

                }

                break;

            case R.id.cancel_attention:

                if(mRecommendAdapter != null){
//                    String followingStr = mRecommendAdapter.getFollowedIdString();
//                    System.out.println("unFollowingStr-->" + followingStr);
//                    if(!TextUtils.isEmpty(followingStr)){
//                        batchCancelFollowing("{user_ids:" + followingStr + "}");
//                    }

                    BatchCancelRequestBody requestBody = new BatchCancelRequestBody(mRecommendAdapter.getFollowedIdint());
                    batchCancelFollowing(requestBody);//("{user_ids:" + unFollowingStr + "}");

//                    String followingStr = mRecommendAdapter.getFollowedIdString();
//                    System.out.println("unFollowingStr-->" + followingStr);
//                    if(!TextUtils.isEmpty(followingStr)){
//                        batchCancelFollowing("{user_ids:" + followingStr + "}");
//                    }

                }

                break;
        }
    }

}
